import Mock from "mockjs";
import { param2Obj } from '../utils/paramsUrl'
// 保存mock数据
var list = []
for(var i=0;i<400;i++){
    var obj={
        id:Mock.mock('@guid'),
        ordernum:Mock.mock('@zip()'),
        ordersort: '课程订单',
        title: '幼儿英语课程',
        money:'¥'+Mock.mock('@natural(60, 800)'),
        // type:Mock.mock('@natural(0, 3)'),
        type:Mock.mock('@integer(0, 3)'),
        time:Mock.mock('@datetime()')

       
    }

    list.push(obj)
}

// console.log(list)
export default {
    getData:config=>{
        // 对应属性获取数据 get
        const { searchVal,page,pageSize } = param2Obj(config.url)
        // 搜索功能
        var newList = list.filter(item=>{
            return  item.ordernum.indexOf(searchVal) != -1 
        })
        // 分页功能
        var newListArr = newList.filter((item,index)=>{

            return (page-1)*pageSize<=index && index < page * pageSize
        })
        // console.log('///',newListArr);//100条
        // console.log('newListArr',list);  400条
        
        return{
            code:200,
            msg:'获取数据成功',
            data:newListArr,
            total:newList.length
        }
        
    },
    
    delData:config=>{
        console.log('config',config);
        const { id } = JSON.parse(config.body)
        for(var i =0;i<list.length;i++){
            if(list[i].id == id){
                list.splice(i,1)
            }
        }
         return {
             code:200,
             msg:'删除成功',
             data:null
         }

    },
    batchData:config=>{
      const { ids } = JSON.parse(config.body)
      var newlist = list.filter(item=>{
          return ids.indexOf(item.id) ==-1//返回不包含的
      })
      list = newlist
      return{
          code:200,
          msg:'批量删除成功',
          data:null
      }

    },
    dialogData:config=>{
        const { id,ordernum,type,money } = JSON.parse(config.body)
        console.log('ordernum',id,ordernum,type,money);
        for(var i =0;i<list.length;i++){
            console.log('length',list.length);
            if(list[i].id === id){
                list[i].ordernum = ordernum
                list[i].type = type
                list[i].money = money
                console.log('ordernum',ordernum);
            }
        }
        return {
            code:200,
            msg:'修改成功',
            data:null
        }
    },
}